package org

import (
	"d-serv-go/server/core/consts"
	"d-serv-go/server/core/db"
	strutils "d-serv-go/server/core/util/stringutils"
)

type roleMgr struct {

}

func NewRoleMgr() *roleMgr {
	return &roleMgr{}
}

func (r roleMgr) GetRoleCodes(userCode string, cmpyCode string, oDeptLevel int) []string {
	publicRole := db.Ctx().GetStr("SY_ORG_ROLE_PUBLIC", "");
	list, _ := db.Ctx().GetExecutor().Query(r.getRoleListSql(userCode, cmpyCode), nil, nil);
	rtn := make([]string, 0)
	for _, bean :=range list {
		rtn = append(rtn, bean.GetStr(consts.COL_ROLE_CODE))
	}
	if (len(publicRole) > 0) {
		rtn = append(rtn, publicRole)
	}
	return rtn;
}

func (r roleMgr) getRoleListSql(userCode string, cmpyCode string) string {
	roleListSql := strutils.NewStringBuilder("select distinct " + consts.COL_ROLE_CODE + " from " + consts.TBL_SY_ORG_ROLE_USER);
	roleListSql.Append(" where " + consts.COL_USER_CODE + "='" + userCode + "'");
	roleListSql.Append(" and " + consts.COL_CMPY_CODE + "='" + cmpyCode + "'");
	roleListSql.Append(" and " + consts.COL_S_FLAG + "=1");

	return roleListSql.ToString();
}